package com.lagou.ipersistence.reflection.property;

import com.lagou.ipersistence.reflection.ReflectionException;

import java.util.Locale;

/**
 * @author Yan Jiahong
 * Created on 2020/11/17
 */
public class PropertyNamer {
    private PropertyNamer() {
    }
    
    /**
     * 根据Getter方法名获取到相应的属性名
     * 比如传入 'getUsername' 输出 'username'
     *
     * @param name 方法名
     * @return 属性名
     */
    public static String methodToProperty(String name) {
        if (name.startsWith("is")) {
            name = name.substring(2);
        } else if (name.startsWith("get") || name.startsWith("set")) {
            name = name.substring(3);
        } else {
            throw new ReflectionException("解析属性名'" + name + "'出错, 原因: 不是以'get', 'set'或者'is'开头");
        }
        if (name.length() == 1 || (name.length() > 1 && Character.isUpperCase(name.charAt(0)))) {
            name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);
        }
        return name;
    }
}
